<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>

    var minElements = function (nums, limit, goal) {
      let all = nums.reduce((a, b) => a + b)
      if (all === goal) {
        return 0
      }
      let diff = Math.abs(goal - all)
      return Math.ceil(diff / limit)
    };
    console.log(minElements([1, -1, 1], 3, -4));
  </script>
</body>

</html>